﻿Imports Mystery
Imports MysteryControls
Imports System.Globalization
Imports System.Windows.Markup
Imports System.Windows.Threading

Class Application

    ' Application-level events, such as Startup, Exit, and DispatcherUnhandledException
    ' can be handled in this file.

    Public Sub Application_DispatcherUnhandledException(sender As Object, e As DispatcherUnhandledExceptionEventArgs)
        Dim var As System.Runtime.InteropServices.COMException = TryCast(e.Exception, System.Runtime.InteropServices.COMException)
        If var IsNot Nothing AndAlso var.ErrorCode = -2147221040 Then e.Handled = True
    End Sub

    Protected Overrides Sub OnStartup(e As StartupEventArgs)
        FrameworkElement.LanguageProperty.OverrideMetadata(GetType(FrameworkElement), New FrameworkPropertyMetadata(
                                                           XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)))
        MyBase.OnStartup(e)
    End Sub

    ' Application-level events, such as Startup, Exit, and DispatcherUnhandledException
    ' can be handled in this file.

    Private Sub setUpPro()
        Dim conf As MysteryConfiguration = getGlobalObject(Of MysteryConfiguration)()
        conf.db_connection_string = My.Settings.dbConnection

        Dim fileroot As String = My.Settings.FileRoot
        If String.IsNullOrEmpty(fileroot) Then
            fileroot = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MysteryData")
        End If

        conf.files_root_folder = New IO.DirectoryInfo(fileroot)
        If Not conf.files_root_folder.Exists Then
            conf.files_root_folder.Create()
        End If


        Dim ms As MysterySession = getGlobalObject(Of MysterySession)()
        Dim au As IAuthetication = getGlobalObject(Of IAuthetication)()

        Dim cc As IContentDispatcher = getGlobalObject(Of IContentDispatcher)()

        If Not cc.containsType(Of User)() Then
            Dim content_creator As IGlobalContentCreator = getGlobalObject(Of IGlobalContentCreator)()
            Dim my_user As User = content_creator.getNewContent(Of User)()
            my_user.username = "uran"
            my_user.password = au.Hashstring("uranio")
            my_user.fullname = "Uranio238"
            cc.Add(my_user)
        End If

    End Sub

    Private Sub logDefaultUser()
        Dim user_data As String = My.Settings.DefaultUser
        If String.IsNullOrEmpty(user_data) Then Exit Sub
        Dim data() As String = user_data.Split(",")
        If data.Count < 2 Then Exit Sub
        Dim username As String = data(0).Trim
        Dim password As String = data(1).Trim
        Dim auth As IAuthetication = getGlobalObject(Of IAuthetication)()
        Dim user As User = auth.autheticate(username, password)
        If user Is Nothing Then Exit Sub
        Dim ms As MysterySession = getGlobalObject(Of MysterySession)()
        ms.user = user
    End Sub


    Private Sub Application_Startup(sender As Object, e As System.Windows.StartupEventArgs) Handles Me.Startup
        getMystery().AssemblyRegister.Register(Me.GetType.Assembly)

        setUpPro()
        logDefaultUser()

        Dim control As New DefaultStartScreenControl
        Dim w As Window = control.instanceWindow()
        w.Show()

        w = (New SayHello).instanceWindow
        w.Show()

    End Sub

End Class
